// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
// #[tauri::command]
// fn greet(name: &str) -> String {
//     format!("Hello, {}! You've been greeted from Rust!", name)
// }

use tauri::SystemTrayEvent;
use tauri::Manager;
fn main() {
    tauri::Builder::default()
        .system_tray(systemtray())
        .on_system_tray_event(|app,event| match event {
            SystemTrayEvent::LeftClick {
                position: _,
                size: _,
                ..
              } => {
                let window = app.get_window("main").unwrap();
                window.show().unwrap();
                window.set_focus().unwrap();
              }
              SystemTrayEvent::RightClick {
                position: _,
                size: _,
                ..
              } => {
                println!("system tray received a right click");
              }
              SystemTrayEvent::DoubleClick {
                position: _,
                size: _,
                ..
              } => {
                let window = app.get_window("main").unwrap();
                window.show().unwrap();
                window.set_focus().unwrap();
              }
              SystemTrayEvent::MenuItemClick { id, .. } => {
                match id.as_str() {
                  "quit" => {
                    std::process::exit(0);
                  }
                  "hide" => {
                    let window = app.get_window("main").unwrap();
                    window.hide().unwrap();
                  }
                  "open"=> {
                    let window = app.get_window("main").unwrap();
                    window.show().unwrap();
                    window.set_focus().unwrap();
                  }
                  _ =>{}
                }
              }
              _ => {}
        })
        .invoke_handler(tauri::generate_handler![])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}


// use mki::Keyboard;
// use std::thread;
// use std::time::Duration;

// fn main1() {

//     mki::register_hotkey(&[Keyboard::LeftControl, Keyboard::Space], || {
//         println!("Ctrl+Space Pressed")
//     });

//     thread::sleep(Duration::from_secs(100));
// }


// system tray 系统托盘菜单
use tauri::SystemTray;
use tauri::{CustomMenuItem, SystemTrayMenu, SystemTrayMenuItem};
fn systemtray()->SystemTray{
    // 创建菜单项
    let quit = CustomMenuItem::new("quit".to_string(), "退出");
    let hide = CustomMenuItem::new("hide".to_string(), "隐藏");
    let open = CustomMenuItem::new("open".to_string(), "打开");

    // 挂载菜单项到系统托盘菜单
    let tray_menu = SystemTrayMenu::new()
    .add_item(quit)
    .add_native_item(SystemTrayMenuItem::Separator)
    .add_item(hide)
    .add_native_item(SystemTrayMenuItem::Separator)
    .add_item(open);
    // 挂在系统托盘菜单到系统托盘
    let tray = SystemTray::new().with_menu(tray_menu);
    return tray;
}